﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Domain;
using S4Store.Service;
using S4Store.Models.SellerReport;
using S4Store.Models.Chart;
using S4Store.Models;

namespace S4Store.Controllers
{
    public class SellerReportController : ReportBaseController
    {
        public ReportService ReportService { get; set; }
        public SellerKpiService SellerKpiService { get; set; }
        public EmployeeService SellerService { get; set; }
        public CarKpiService CarKpiService { get; set; }


        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                var report = ReportService.GetSellerReport(Month.Year, Month.Month, Store.Id);
                if (report.IsCarKpiMonthConfigConfirmed)
                {
                    model.IsMonthConfigConfirmed = true;
                    model.Items.Add(new IndexModel.ItemModel
                    {
                        Name = "首次来店登记率",
                        KHMB = report.SellerPerformanceStandard.SCLDDJLV,
                        XSSJ = report.SCLDDJLV,
                        Url=this.Url.Action("SellerDataItem",new {Month=Month.ToString("yyyy-MM-01"), type=(int)SellerDataItemType.首次来店登记率 })
                    });
                    model.Items.Add(new IndexModel.ItemModel
                    {
                        Name = "试乘试驾率",
                        KHMB = report.SellerPerformanceStandard.SCSJLV,
                        XSSJ = report.SCSJLV,
                        Url = this.Url.Action("SellerDataItem", new { Month = Month.ToString("yyyy-MM-01"), type = (int)SellerDataItemType.试乘试驾率 })
                    });
                    model.Items.Add(new IndexModel.ItemModel
                    {
                        Name = "试驾未成交率",
                        KHMB = report.SellerPerformanceStandard.SCSJCJLV,
                        XSSJ = report.SCSJCJLV,
                        Url = this.Url.Action("SellerDataItem", new { Month = Month.ToString("yyyy-MM-01"), type = (int)SellerDataItemType.试驾未成交率 })
                    });

                    model.Items.Add(new IndexModel.ItemModel
                    {
                        Name = "登记客户成交率",
                        KHMB = report.SellerPerformanceStandard.DJKHCJLV,
                        XSSJ = report.DJKHCJLV,
                        Url = this.Url.Action("SellerDataItem", new { Month = Month.ToString("yyyy-MM-01"), type = (int)SellerDataItemType.登记客户成交率 })

                    });

                    model.Items.Add(new IndexModel.ItemModel
                    {
                        Name = "上月客户转化率",
                        KHMB = report.SellerPerformanceStandard.SYKHZHLV,
                        XSSJ = report.SYKHZHLV,
                        Url = this.Url.Action("SellerDataItem", new { Month = Month.ToString("yyyy-MM-01"), type = (int)SellerDataItemType.上月客户转化率 })

                    });

                    model.Items.Add(new IndexModel.ItemModel
                    {
                        Name = "超限价格成交率",
                        KHMB = report.SellerPerformanceStandard.CXJGCJLV,
                        XSSJ = report.CXJGCJLV,
                        Url = this.Url.Action("SellerDataItem", new { Month = Month.ToString("yyyy-MM-01"), type = (int)SellerDataItemType.超限价格成交率 })

                    });
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "Index";
            model.BarModel.Controller = "SellerReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            model.BarModel.PlusItem = "<a target='navtab' class='historyrecord' title='销售顾问执行过程KPI' href='" + Url.Action("BySeller") + "' ><span>人员查询</span></a>";
            return View(model);
        }


        public ActionResult SellerDataItem(SellerDataItemType type)
        {
            SellerDataItemModel model = new SellerDataItemModel();
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(SellerKpiService);
                model.ItemType = type;
                model.IsMonthConfigConfirmed = SellerKpiService.IsMonthConfiged(Month.Year, Month.Month, Store.Id);
                if (model.IsMonthConfigConfirmed)
                {
                    foreach (var seller in Store.GetEnbleEmployee(EmployeeType.FieldSaler))
                    {
                        var report = ReportService.GetSellerPersonReport(Month.Year, Month.Month, Store.Id, seller.Id);
                        
                        model.SellerPersonReports.Add(report);
                    }
                }
                model.SellerReportModel = ReportService.GetSellerReport(Month.Year, Month.Month, Store.Id);
                t.Commit();
            }

            model.LineModel = getLine(type, Start.Value,End.Value, Store.Id);
            this.InjectReportModel(model);
            model.BarModel.Action = "SellerDataItem";
            model.BarModel.Controller = "SellerReport";
            model.BarModel.PlusItem = "<input type='hidden' name='type' value='" + type.ToInt() + "'/>";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Month);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }

        private Line getLine(SellerDataItemType type, DateTime start,DateTime end, int storeid)
        {
            Line model = new Line();
            model.YAxis = "比例值(%)";
            model.Name = "销售部半年平均" + type.ToString() + "走势";
            model.XAxis = getXaxis(type, start, end);
            model.Serieses = getSerieses(type, start, end, storeid);
            return model;
        }

        private List<ChartSeries> getSerieses(SellerDataItemType type, DateTime start, DateTime end, int storeid)
        {
            List<ChartSeries> series = new List<ChartSeries>();
            ChartSeries smb = new ChartSeries();
            smb.Data = new List<decimal>();
            ChartSeries ssj = new ChartSeries();
            ssj.Data = new List<decimal>();
            switch (type)
            {
                case SellerDataItemType.首次来店登记率:
                    {
                        smb.Name = "目标登记率";
                        ssj.Name = "实际登记率";
                        using (var t = new Transtion())
                        {
                            t.Inject(ReportService);
                            for (DateTime dt = start; dt <= end; dt = dt.AddMonths(1))
                            {
                                var report = ReportService.GetSellerReport(dt.Year, dt.Month, storeid);                                
                                smb.Data.Add(report.SellerPerformanceStandard.SCLDDJLV);
                                ssj.Data.Add( report.SCLDDJLV);
                            }
                            t.Commit();
                        }
                        break;
                    }
                case SellerDataItemType.试乘试驾率:
                    {
                        smb.Name = "目标试乘试驾率";
                        ssj.Name = "实际试乘试驾率";
                        using (var t = new Transtion())
                        {
                            t.Inject(ReportService);
                            for (DateTime dt = start; dt <= end; dt = dt.AddMonths(1))
                            {
                                var report = ReportService.GetSellerReport(dt.Year, dt.Month, storeid);
                                smb.Data.Add(report.SellerPerformanceStandard.SCSJLV);
                                ssj.Data.Add(report.SCSJLV);
                            }
                            t.Commit();
                        }
                        break;
                    }
                case SellerDataItemType.试驾未成交率:
                    {
                        smb.Name = "目标试驾未成交率";
                        ssj.Name = "实际试驾未成交率";
                        using (var t = new Transtion())
                        {
                            t.Inject(ReportService);
                            for (DateTime dt = start; dt <= end; dt = dt.AddMonths(1))
                            {
                                var report = ReportService.GetSellerReport(dt.Year, dt.Month, storeid);
                                smb.Data.Add(report.SellerPerformanceStandard.SCSJCJLV);
                                ssj.Data.Add(report.SCSJCJLV);
                            }
                            t.Commit();
                        }
                        break;
                    }
                case SellerDataItemType.登记客户成交率:
                    {
                        smb.Name = "目标登记客户成交率";
                        ssj.Name = "实际登记客户成交率";
                        using (var t = new Transtion())
                        {
                            t.Inject(ReportService);
                            for (DateTime dt = start; dt <= end; dt = dt.AddMonths(1))
                            {
                                var report = ReportService.GetSellerReport(dt.Year, dt.Month, storeid);
                                smb.Data.Add(report.SellerPerformanceStandard.DJKHCJLV);
                                ssj.Data.Add(report.DJKHCJLV);
                            }
                            t.Commit();
                        }
                        break;
                    }
                case SellerDataItemType.上月客户转化率:
                    {
                        smb.Name = "目标上月客户转化率";
                        ssj.Name = "实际上月客户转化率";
                        using (var t = new Transtion())
                        {
                            t.Inject(ReportService);
                            for (DateTime dt = start; dt <= end; dt = dt.AddMonths(1))
                            {
                                var report = ReportService.GetSellerReport(dt.Year, dt.Month, storeid);
                                smb.Data.Add(report.SellerPerformanceStandard.SYKHZHLV);
                                ssj.Data.Add(report.SYKHZHLV);
                            }
                            t.Commit();
                        }
                        break;
                    }
                case SellerDataItemType.超限价格成交率:
                    {
                        smb.Name = "目标超限率";
                        ssj.Name = "实际超限率";
                        using (var t = new Transtion())
                        {
                            t.Inject(ReportService);
                            for (DateTime dt = start; dt <= end; dt = dt.AddMonths(1))
                            {
                                var report = ReportService.GetSellerReport(dt.Year, dt.Month, storeid);
                                smb.Data.Add(report.SellerPerformanceStandard.CXJGCJLV);
                                ssj.Data.Add(report.CXJGCJLV);
                            }
                            t.Commit();
                        }
                        break;
                    }
            }
            series.Add(smb);
            series.Add(ssj);
            return series;
        }

        private List<String> getXaxis(SellerDataItemType type, DateTime start, DateTime end)
        {
            List<String> axis = new List<string>();
            for (DateTime dt = start; dt <= end; dt = dt.AddMonths(1))
            {
                axis.Add(dt.ToString("yy-M"));
            }
            return axis;
        }
        
        public ActionResult BySeller()
        {
            BySellerModel model = new BySellerModel();
            model.ChartData.Name = "销售顾问执行过程KPI";
            model.ChartData.YAxis = "进度百分比(%)";
            var seriestime = new ChartSeries();
            seriestime.Name = "时间进度";
            var seriessale = new ChartSeries();
            seriessale.Name = "销量进度";
            model.ChartData.Serieses.Add(seriestime);
            model.ChartData.Serieses.Add(seriessale);
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(SellerKpiService);
                model.IsMonthConfigConfirmed = SellerKpiService.IsMonthConfiged(Month.Year, Month.Month, Store.Id);
                foreach (var seller in Store.GetEnbleEmployee(EmployeeType.FieldSaler))
                {
                    model.ChartData.XAxis.Add(seller.Name);                    
                    var report = ReportService.GetSellerPersonReport(Month.Year, Month.Month, Store.Id, seller.Id);
                    seriestime.Data.Add(report.RateOfTime);
                    seriessale.Data.Add(report.SaleRate);

                    //差值
                    var value = report.SaleCount - report.SaleTarget * report.RateOfTime / 100;
                    //RedGreenIndex index = new RedGreenIndex();
                    //index.Size = RedGreenIndex.IndexSize.Middle;
                    //if (value > 0)
                    //{
                    //    index.Type = RedGreenIndex.IndexType.Green;
                    //}
                    //else
                    //{
                    //    index.Type = RedGreenIndex.IndexType.Red;
                    //    index.Value = ((int)value).ToString();
                    //}
                    //index.Title =seller.Name+" 销量时间进度差过程KPI";
                    //index.Url = Url.Action("ByPerson", new { SearchSellerId=seller.Id });
                    //model.RedGreenIndexs.Add(index);

                    AdvIndex idx = new AdvIndex();
                    idx.Item1Name = "实际订单总数";
                    idx.Item1Value = report.OrderCount.ToString();
                    idx.Item2Name = "实际交车总数";
                    idx.Item2Value = report.SaleCount.ToString();
                    idx.Size = AdvIndex.IndexSize.Middle;
                    idx.Tip = seller.Name+"销售执行过程KPI";
                    idx.Title = "实际交车总数与目标销量的差值";
                    idx.Type = (value >= 0 ? AdvIndex.IndexType.Green : AdvIndex.IndexType.Red);
                    idx.Url = Url.Action("ByPerson", new { SearchSellerId = seller.Id });
                    idx.Value = value.toUpInt();
                    model.AdvIndexs.Add(idx);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "BySeller";
            model.BarModel.Controller = "SellerReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult ByPerson()
        {
            ByPersonModel model = new ByPersonModel();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(SellerService);
                t.Inject(SellerKpiService);
                t.Inject(CarKpiService);
                model.Seller = SellerService.GetQuery().FirstOrDefault(x => x.Id == SearchSellerId.Value);
                if (model.Seller.Type == EmployeeType.TelSaler)
                {
                    return RedirectToAction("ByPerson", "TelSaleReport");
                }
                model.IsMonthConfigConfirmed = SellerKpiService.IsMonthConfiged(Month.Year, Month.Month, Store.Id);
                //foreach (var seller in Store.EnbleSellers)
                //{
                var brands = CarKpiService.GetCarbrandsForStoreMonth(Month.Year, Month.Month, Store.Id).ToList();
                    foreach (var brand in brands)
                    {
                        var report = ReportService.GetSellerPersonBrandReport(Month.Year, Month.Month, Store.Id, SearchSellerId.Value, brand.Id);
                        report.Seller = model.Seller;
                        report.CarBrand = brand;
                        model.SellerKpiPersonBodyModel.Reports.Add(report);
                    }
                //}
                    model.SellerKpiPersonBodyModel.PersonReport = ReportService.GetSellerPersonReport(Month.Year, Month.Month, Store.Id, SearchSellerId.Value);
                    model.SellerKpiPersonBodyModel.StoreReport = ReportService.GetSellerReport(Month.Year, Month.Month, Store.Id);
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "ByPerson";
            model.BarModel.Controller = "SellerReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult Customer()
        {
            HistoryModel model = new HistoryModel();
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }

            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(SellerService);
                t.Inject(SellerKpiService);
                t.Inject(CarKpiService);

                model.Seller = SellerService.GetQuery().FirstOrDefault(x => x.Id == SearchSellerId.Value);
                for (DateTime dt = Start.Value; dt <=End; dt = dt.AddMonths(1))
                {
                    var report = ReportService.GetSellerPersonReport(dt.Year, dt.Month, Store.Id, SearchSellerId.Value);
                    report.Seller = model.Seller;
                    model.Reports.Add(report);
                }
                t.Commit();
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "Customer";
            model.BarModel.Controller = "SellerReport";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }
    
    }
}
